//
//  HWProgressHUD.m
//  GTMC_CarOwner_Yonyou
//
//  Created by hongw on 16/4/27.
//  Copyright © 2016年 hongw. All rights reserved.
//

#import "HWProgressHUD.h"
//动画时间
#define kAnimationDuration      1

@interface HWProgressHUD()

@property (strong, nonatomic) HWAnimationView *animationView;

@end

@implementation HWProgressHUD

+ (HWProgressHUD*)sharedView {
    static dispatch_once_t once;
    static HWProgressHUD *sharedView;
    dispatch_once(&once, ^ {
        sharedView = [[self alloc] init];
    });
    return sharedView;
}

+ (void)showHUDAddedTo:(UIView*)superview{
    HWProgressHUD *HUD = [self sharedView];
    HUD.frame = superview.bounds;
    [HUD removeFromSuperview];
    [superview addSubview:HUD];
    NSAssert(superview, @"superview不能为空");
    HUD.translatesAutoresizingMaskIntoConstraints = NO;
    [superview addConstraint:[NSLayoutConstraint constraintWithItem:HUD
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:superview
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1.0
                                                           constant:0]];
    
    [superview addConstraint:[NSLayoutConstraint constraintWithItem:HUD
                                                          attribute:NSLayoutAttributeBottom
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:superview
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1.0
                                                           constant:0]];
    
    [superview addConstraint:[NSLayoutConstraint constraintWithItem:HUD
                                                          attribute:NSLayoutAttributeLeading
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:superview
                                                          attribute:NSLayoutAttributeLeading
                                                         multiplier:1.0
                                                           constant:0]];
    
    [superview addConstraint:[NSLayoutConstraint constraintWithItem:HUD
                                                          attribute:NSLayoutAttributeTrailing
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:superview
                                                          attribute:NSLayoutAttributeTrailing
                                                         multiplier:1.0
                                                           constant:0]];
    
    [HUD addSubview:HUD.animationView];
    
    
    UIImage *earthImage = [UIImage imageNamed:@"loading"];
    UIView *animationView = HUD.animationView;
    animationView.translatesAutoresizingMaskIntoConstraints = NO;
    [HUD addConstraint:[NSLayoutConstraint constraintWithItem:animationView
                                                          attribute:NSLayoutAttributeCenterX
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:HUD
                                                          attribute:NSLayoutAttributeCenterX
                                                         multiplier:1.0
                                                           constant:0]];
    
    [HUD addConstraint:[NSLayoutConstraint constraintWithItem:animationView
                                                    attribute:NSLayoutAttributeCenterY
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:HUD
                                                    attribute:NSLayoutAttributeCenterY
                                                   multiplier:1.0
                                                     constant:0]];
    
    [HUD addConstraint:[NSLayoutConstraint constraintWithItem:animationView
                                                    attribute:NSLayoutAttributeWidth
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:nil
                                                    attribute:NSLayoutAttributeNotAnAttribute
                                                   multiplier:1.0
                                                     constant:earthImage.size.width]];
    
    [HUD addConstraint:[NSLayoutConstraint constraintWithItem:animationView
                                                    attribute:NSLayoutAttributeHeight
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:nil
                                                    attribute:NSLayoutAttributeNotAnAttribute
                                                   multiplier:1.0
                                                     constant:earthImage.size.height]];
}

+ (void)dismissFromSupview{
    [UIView animateWithDuration:0.25 animations:^{
        [self sharedView].animationView.alpha = 0;
        [self sharedView].animationView.transform = CGAffineTransformMakeScale(0.8, 0.8);
    } completion:^(BOOL finished) {
        [[self sharedView] removeFromSuperview];
        [[self sharedView].animationView removeFromSuperview];
        [self sharedView].animationView = nil;
    }];
}

- (HWAnimationView *)animationView{
    if (!_animationView) {
        _animationView = [[HWAnimationView alloc] init];
    }
    return _animationView;
}

@end


@interface HWAnimationView()
//@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) CALayer *animationLayer;
@end

@implementation HWAnimationView

- (void)willMoveToSuperview:(UIView *)newSuperview{
    if (newSuperview) {
        [self layoutAnimatedLayer];
    }else{
        [_animationLayer removeFromSuperlayer];
        _animationLayer = nil;
    }
}

- (void)layoutAnimatedLayer{
    CALayer *layer = self.animationLayer;
    [self.layer addSublayer:layer];
}

- (CALayer *)animationLayer{
    if (!_animationLayer) {
        _animationLayer = [[CALayer alloc] init];
        UIImage *image = [UIImage imageNamed:@"loading"];
        _animationLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height);
        _animationLayer.contents = (id)image.CGImage;
        
        CAMediaTimingFunction *linearCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        
        CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        rotationAnimation.toValue = [NSNumber numberWithFloat:2 * M_PI];
        rotationAnimation.duration = kAnimationDuration;
        rotationAnimation.repeatCount = INFINITY;
        rotationAnimation.timingFunction = linearCurve;
        rotationAnimation.removedOnCompletion = NO;
        rotationAnimation.fillMode = kCAFillModeForwards;
        rotationAnimation.autoreverses = NO;
        [_animationLayer addAnimation:rotationAnimation forKey:@"transform.rotation"];
    }
    return _animationLayer;
}

- (void)setFrame:(CGRect)frame{
    if (CGRectEqualToRect(frame, super.frame)) {
        [super setFrame:frame];
        if (self.superview) {
            [self layoutAnimatedLayer];
        }
    }
}

@end
